package com.gct.controller;

import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gct.controller.base.BaseController;
import com.gct.domain.Theme;
import com.gct.domain.wrapper.DomainWrapper;
import com.gct.service.ThemeService;

@RestController
@RequestMapping("/theme")
public class ThemeController extends BaseController {
    @Autowired
    private ThemeService themeService;

    @RequestMapping(value = "/themes/{page}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Theme> queryThemes(@PathVariable("page") long page) {
        try{
            DomainWrapper<Theme> dmw = new DomainWrapper<Theme>();
            dmw.setData(themeService.getThemes());
//            dmw.setData(null);
//            throw new RuntimeException("throw");
            return dmw;
        } catch (Throwable e) {
            return this.<Theme>processException(e);
        }
    }

    @RequestMapping(value = "/artsintheme/{id}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Theme> queryArtsInTheme(@PathVariable("id") int id) {
        try {
            DomainWrapper<Theme> dmw = new DomainWrapper<Theme>();
            List<Theme> themes = new LinkedList<Theme>();
            themes.add(themeService.getArtsInTheme(id));
            dmw.setData(themes);
            return dmw;
        } catch (Throwable e) {
            return this.<Theme>processException(e);
        }
    }

    @RequestMapping(value = "/artsinthemesec/{id}/{seconds}/{page}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Theme> queryArtsInThemeAndSeconds(@PathVariable("id") int id, @PathVariable("seconds") int seconds, @PathVariable("page") long page) {
        try {
            DomainWrapper<Theme> dmw = new DomainWrapper<Theme>();
            List<Theme> themes = new LinkedList<Theme>();
            themes.add(themeService.getArtsInThemeWithDuration(id, seconds));
            dmw.setData(themes);
            return dmw;
        } catch (Throwable e) {
            return this.<Theme>processException(e);
        }
    }

    @RequestMapping(value = "/themeart/{artId}",method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public DomainWrapper<Theme> themeArt(@PathVariable("artId") int artId,  @RequestBody String tIds) {
        try {
            if(null == tIds)
                return new DomainWrapper<Theme>();

            List<Integer> themeIds = new LinkedList<Integer>();
            String[] ids = tIds.split(":")[1].split("}")[0].split(",");
            ids[0] = ids[0].substring(1);
            ids[ids.length - 1] = ids[ids.length - 1].substring(0,1);
            for(String id : ids) {
                themeIds.add(Integer.valueOf(id));
                logger.info(id);
            }
            themeService.voteArtThemes(artId, themeIds);
            DomainWrapper<Theme> dmw = new DomainWrapper<Theme>();
            return dmw;
        } catch (Throwable e) {
            return this.<Theme>processException(e);
        }
    }
}
